package com.ggh.jinjilive.ui.mine.members;

import android.os.Bundle;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.BarUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityMembersBinding;
import com.ggh.jinjilive.mobile.mine.MembersMobile;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.ui.mall.order.OrderActivity;
import com.ggh.jinjilive.ui.mall.order.OrderFragment;
import com.ggh.jinjilive.ui.mine.MineViewModel;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.gyf.immersionbar.ImmersionBar;

import org.jetbrains.annotations.NotNull;

import java.util.List;


@Route(path = ARouterUtils.MembersActivity)
public class MembersActivity extends BaseActivity<ActivityMembersBinding> {
    //VM
    private MineViewModel mineViewModel;
    //几个会员
    private String[] mFragmentTitles = {"视频会员", "访客会员","地区会员","迈炫会员","vip会员"};
    private ProfitOrderAdapter profitOrderAdapter;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).keyboardEnable(true).init();
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        mineViewModel = new ViewModelProvider(this,viewModelFactory).get(MineViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_members;
    }

    @Override
    public void initView() {
        super.initView();
        setWhiteActionBarStyle("我的会员",true,true);
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);

    }

    @Override
    public void initData() {
        super.initData();
        //获取会员列表
        mineViewModel.infoViplist();

    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //获取会员列表
        mineViewModel.mMembersMobileList.observe(this, new Observer<List<MembersMobile>>() {
            @Override
            public void onChanged(List<MembersMobile> membersMobileList) {
                if(membersMobileList.size() > 0){
                    //适配器
                    profitOrderAdapter = new ProfitOrderAdapter(getSupportFragmentManager());
                    binding.vp.setAdapter(profitOrderAdapter);
                    binding.stlOrder.setViewPager(binding.vp, mFragmentTitles);
                    binding.vp.setCurrentItem(0);
                }
            }
        });
    }

    class ProfitOrderAdapter extends FragmentPagerAdapter {

        public ProfitOrderAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {


            return new MembersFragment(mFragmentTitles[position],mineViewModel.mMembersMobileList.getValue());
        }

        @Override
        public int getCount() {
            return mFragmentTitles.length;
        }

        @Override
        public void destroyItem(@NonNull @NotNull ViewGroup container, int position, @NonNull @NotNull Object object) {
//            super.destroyItem(container, position, object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles[position];
        }

        @Override
        public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            super.setPrimaryItem(container, position, object);
        }
    }
}
